package org.jeecg.modules.performance.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.performance.entity.PerformanceMonthDetail;
import org.jeecg.modules.performance.entity.PerformanceUserAdjust;
import org.jeecg.modules.performance.entity.PerformanceUserLog;
import org.jeecg.modules.performance.service.IPerformanceMonthDetailService;
import org.jeecg.modules.performance.service.IPerformanceUserAdjustService;
import org.jeecg.modules.performance.service.IPerformanceUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: performance
 * @Author: jeecg-boot
 * @Date: 2021-05-19
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "performance")
@RestController
@RequestMapping("/performance/performanceUserAdjust")
public class PerformanceUserAdjustController extends JeecgController<PerformanceUserAdjust, IPerformanceUserAdjustService> {
	@Autowired
	private IPerformanceUserAdjustService performanceUserAdjustService;
	@Autowired
	private IPerformanceMonthDetailService performanceMonthDetailService;
	@Autowired
	private IPerformanceUserLogService performanceUserLogService;

	/**
	 * 分页列表查询
	 *
	 * @param performanceUserAdjust
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "performance-分页列表查询")
	@ApiOperation(value = "performance-分页列表查询", notes = "performance-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PerformanceUserAdjust performanceUserAdjust,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PerformanceUserAdjust> queryWrapper = QueryGenerator.initQueryWrapper(performanceUserAdjust, req.getParameterMap());
		Page<PerformanceUserAdjust> page = new Page<PerformanceUserAdjust>(pageNo, pageSize);
		IPage<PerformanceUserAdjust> pageList = performanceUserAdjustService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @return
	 */
	@AutoLog(value = "performance-添加")
	@ApiOperation(value = "performance-添加", notes = "performance-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody List<PerformanceUserAdjust> performanceUserAdjustList) {
		Float totalAmount = 0f;

		List<PerformanceUserLog> performanceUserLogList = new ArrayList<>();
		for (PerformanceUserAdjust performanceUserAdjust : performanceUserAdjustList) {

			if(performanceUserAdjust.getAdjustAmount() <=0){
				throw new JeecgBootException("请输入正确的调整金额");

			}
			PerformanceUserLog performanceUserLog = new PerformanceUserLog();
			BeanUtils.copyProperties(performanceUserAdjust, performanceUserLog);
			if (performanceUserAdjust.getOpType().equals(1)) {
				performanceUserLog.setLogName("行政调整");
			} else {
				performanceUserLog.setLogName("部门调整");

			}
			performanceUserLogList.add(performanceUserLog);
			if (performanceUserAdjust.getAdjustType().equals(1)) {
				//加 显示是减
				totalAmount -= performanceUserAdjust.getAdjustAmount();
			} else {
				totalAmount += performanceUserAdjust.getAdjustAmount();

			}

		}
		Float totalAdjust = 0f;
		PerformanceMonthDetail performanceMonthDetail = performanceMonthDetailService.getById(performanceUserAdjustList.get(0).getDetailId());

		if (performanceUserAdjustList.get(0).getOpType().equals(1)) {
			totalAdjust = performanceMonthDetailService.getTotalAdjust(performanceMonthDetail.getPerforMonthId(), null,1);

		} else {
			//部门调整
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			totalAdjust = performanceMonthDetailService.getTotalAdjust(performanceMonthDetail.getPerforMonthId(), sysUser.getDepartIds(),2);

		}
		if (totalAdjust + totalAmount < 0) {
			throw new JeecgBootException("该部门奖金微调超出额度，请重新调整");
		}
		performanceUserAdjustService.saveBatch(performanceUserAdjustList);
		performanceUserLogService.saveBatch(performanceUserLogList);

		//todo 插入日志表
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param performanceUserAdjust
	 * @return
	 */
	@AutoLog(value = "performance-编辑")
	@ApiOperation(value = "performance-编辑", notes = "performance-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PerformanceUserAdjust performanceUserAdjust) {
		performanceUserAdjustService.updateById(performanceUserAdjust);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "performance-通过id删除")
	@ApiOperation(value = "performance-通过id删除", notes = "performance-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		performanceUserAdjustService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "performance-批量删除")
	@ApiOperation(value = "performance-批量删除", notes = "performance-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.performanceUserAdjustService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "performance-通过id查询")
	@ApiOperation(value = "performance-通过id查询", notes = "performance-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		PerformanceUserAdjust performanceUserAdjust = performanceUserAdjustService.getById(id);
		return Result.ok(performanceUserAdjust);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param performanceUserAdjust
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, PerformanceUserAdjust performanceUserAdjust) {
		return super.exportXls(request, performanceUserAdjust, PerformanceUserAdjust.class, "performance");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, PerformanceUserAdjust.class);
	}

}
